﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using JsalesModel;

namespace JSales.ModuleProduct
{
    public partial class XtraFormLevel : DevExpress.XtraEditors.XtraForm
    {
        public XtraFormLevel()
        {
            InitializeComponent();
        }

        private string mode=null;
        enum EditMode
        {
            Normal = 0, Edit = 1, Add = 2
        }
        private void EnterEditMode(EditMode editMode)
        {
            switch(editMode)
            {
                case EditMode.Normal:
                    {
                        mode = "Normal";

                        barButtonItemAdd.Enabled = true;
                        barButtonItemEdit.Enabled = true;
                        barButtonItemDelete.Enabled = true;
                        barButtonItemSave.Enabled = false;
                        barButtonItemCancel.Enabled = false;

                        tbLevel.Enabled = false;
                        tbLevelName.Enabled = false;

                        gridView1.OptionsBehavior.Editable = false;
                        gridView1.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.False;
                        //gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
                    }
                    break;
                case EditMode.Edit:
                    {
                        mode = "Edit";

                        barButtonItemAdd.Enabled = false;
                        barButtonItemEdit.Enabled = false;
                        barButtonItemDelete.Enabled = false;
                        barButtonItemSave.Enabled = true;
                        barButtonItemCancel.Enabled = true;

                        tbLevel.Enabled = false;
                        tbLevelName.Enabled = true;

                        gridView1.OptionsBehavior.Editable = false;
                        gridView1.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.False;
                        //gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
                    }
                    break;
                case EditMode.Add:
                    {
                        mode = "Add";

                        barButtonItemAdd.Enabled = false;
                        barButtonItemEdit.Enabled = false;
                        barButtonItemDelete.Enabled = false;
                        barButtonItemSave.Enabled = true;
                        barButtonItemCancel.Enabled = true;

                        tbLevel.Enabled = true;
                        tbLevelName.Enabled = true;

                        gridView1.OptionsBehavior.Editable = false;
                        gridView1.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.False;
                        //gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
                    }
                    break;

            }
        }
        BindingSource  bindingSource=new BindingSource();
        
        private void XtraFormLevel_Load(object sender, EventArgs e)
        {
            EnterEditMode(EditMode.Normal);

            var dbConText = new JYEntities();
            IQueryable<TbLevel> levels;

            if (dbConText.TbLevels.Any())
            {
                levels = dbConText.TbLevels;
                bindingSource.DataSource = levels;
                gridControl1.DataSource = bindingSource;

                gridView1.Columns["LEVELID"].Caption = "级别";
                gridView1.Columns["LEVELNAME"].Caption = "级别名称";
                gridView1.Columns["ISDELETED"].Caption = "是否已停用";
            }

        }

        private void barButtonItemAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            EnterEditMode(EditMode.Add);
        }

        private void barButtonItemEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            EnterEditMode(EditMode.Edit);

//            int selectedHandle;
//            selectedHandle = this.gridView1.GetSelectedRows()[0];
//            string selectcell = this.gridView1.GetRowCellValue(selectedHandle, "LEVELID").ToString();
//            if (!string.IsNullOrEmpty(selectcell))
//            {
//                var dbConText = new JYEntities();
//                IQueryable<TbLevel> levels = from VAR in dbConText.TbLevels
//                                 where VAR.LEVELID == selectcell[0]
//                                 select VAR;
//                if (levels.Any())
//                {
//                    tempLevel = levels.FirstOrDefault();
//                    tbLevel.Text = tempLevel.LEVELID.ToString();
//                    tbLevelName.Text = tempLevel.LEVELNAME;
//                }
//            }
        }

        private void barButtonItemSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (mode)
            {
                case "Normal":
                    {
                        MessageBox.Show("当前处理非编辑模式！");
                    }
                    break;
                case "Add":
                    {
                        var dbConText = new JYEntities();
                        TbLevel level= new TbLevel();
                        level.LEVELID = tbLevel.Text[0];
                        level.LEVELNAME = tbLevelName.Text;
                        level.ISDELETED = false;

                        dbConText.Add(level);
                        dbConText.SaveChanges();

                        XtraFormLevel_Load(sender,e);
                    }
                    break;
                case "Edit":
                    {
                        int selectedHandle;
                        selectedHandle = this.gridView1.GetSelectedRows()[0];
                        string selectcell = this.gridView1.GetRowCellValue(selectedHandle, "LEVELID").ToString();
                        if (!string.IsNullOrEmpty(selectcell))
                        {
                            var dbConText = new JYEntities();
                            IQueryable<TbLevel> levels = (from i in dbConText.TbLevels
                                                 where i.LEVELID==selectcell[0]
                                                 select i);
                            if (levels.Any())
                            {
                                levels.FirstOrDefault().LEVELNAME=tbLevelName.Text;
                                dbConText.SaveChanges();
                            }

                        }
                        XtraFormLevel_Load(sender,e);
                    }
                    break;
            }
        }

        private void barButtonItemCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            EnterEditMode(EditMode.Normal);
        }

        private void barButtonItemDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int selectedHandle;
            selectedHandle = this.gridView1.GetSelectedRows()[0];
            string selectcell=this.gridView1.GetRowCellValue(selectedHandle, "LEVELID").ToString();
            if (!string.IsNullOrEmpty(selectcell))
            {
                var dbConText = new JYEntities();
                TbLevel level = (from VAR in dbConText.TbLevels
                                where VAR.LEVELID == selectcell[0]
                                select VAR).FirstOrDefault();
                ;
                level.ISDELETED = true;
                dbConText.SaveChanges();
            }

            XtraFormLevel_Load(sender, e);
        }

        private void barButtonItemFind_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.FindPanelVisible = true;
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
//            tbLevel.DataBindings.Clear();
//            tbLevel.DataBindings.Add("Text", bindingSource, "LEVELID");

            tbLevelName.DataBindings.Clear();
            tbLevelName.DataBindings.Add("Text", bindingSource, "LEVELNAME");
            
            gridControl1.Refresh();
        }
    }



}